﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using LocalSettings;
using QuickTasks.ViewModels.Data;
using QuickTasks.Views.BackGrounds;
using Microsoft.Phone.Net.NetworkInformation;
using Microsoft.Advertising.Mobile.UI;
using System.Device.Location;
using Common.Library;

namespace QuickTasks.Views.Places
{
    public partial class AddPlacesSettings : PhoneApplicationPage
    {
        Distance d = new Distance();
       
        #region constructor

        public AddPlacesSettings()
        {
            InitializeComponent();
          

        }

        #endregion

        #region Pageload
        
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            string rang = IsolatedSettings.getSettingsStringValue("range");
            try
            {
                if (rang == "")
                {
                    TbxRange.Text = "20";
                }
                else
                {
                    TbxRange.Text = rang;
                }

                bool GeoCodeLocation1 = IsolatedSettings.getSettingsBoolValue("GeoLocationStatus1");
                GpsLocationToggleSwitch1.IsChecked = GeoCodeLocation1;
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in  PhoneApplicationPage_Loaded event In AddPlacesSettings.xaml page", ex);
            }
        }

        #endregion
       
        
        protected void MyStatusChanged()
        {
            try
            {
                IsolatedSettings.saveSettings("GeoLocationStatus", "true");
                if (IsolatedSettings.getSettingsStringValue("Locationserviceon") == "false")
                {

                   
                    MessageBoxResult resultLocation = MessageBox.Show("To use this feature Location Service should be enabled.press OK to enable it", "Add Location", MessageBoxButton.OKCancel);
                    if (resultLocation == MessageBoxResult.OK)
                    {

                        GpsLocationToggleSwitch1.Content = "Off";
                       
                        IsolatedSettings.saveSettings("GeoLocationStatus1", "false");
                        GpsLocationToggleSwitch1.IsChecked = false;
                    }
                    else
                    {

                    }
                }
                else
                {

                   
                    GpsLocationToggleSwitch1.Content = "On";
                    IsolatedSettings.saveSettings("GeoLocationStatus1", "true");
                    
                  
                    GpsLocationToggleSwitch1.IsChecked = true;
                }
                

            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in MyStatusChanged method In Geo file", ex);
            }
        }
        #region Events
        private void GpsLocationToggleSwitch1_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                bool enabled = true;
                GeoCoordinateWatcher geo = new GeoCoordinateWatcher();
                geo.Start();
                if (geo.Permission == GeoPositionPermission.Denied)
                {
                    enabled = false;
                }
                geo.Stop();
                geo.Dispose();
                if (enabled)
                {
                    GpsLocationToggleSwitch1.Content = "On";
                    SettingsHelper.Save("GeoLocationStatus", "true");
                }
                else
                {
                    MessageBoxResult result = MessageBox.Show("Your location services are turned off.To turn them on, go to location settings?", "Location services are off", MessageBoxButton.OK);

                    GpsLocationToggleSwitch1.IsChecked = false;

                }
                MyStatusChanged();
                    
              
                
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in  GpsLocationToggleSwitch1_Checked event In AddPlacesSettings.xaml page", ex);
            }
        }

        private void GpsLocationToggleSwitch1_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                GpsLocationToggleSwitch1.Content = "Off";
                SettingsHelper.Save("GeoLocationStatus", "false");
                toggleswitchRunUnderLoock.Content = "Off";
                SettingsHelper.Save("runUnderLock", "false");
                toggleswitchRunUnderLoock.IsChecked = false;
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in  GpsLocationToggleSwitch1_Unchecked event In AddPlacesSettings.xaml page", ex);
            }
        }


       
       


        #endregion

        private void TbxRange_LostFocus(object sender, RoutedEventArgs e)
        {
            if (Convert.ToInt32(TbxRange.Text) < 20)
            {
                tblkRangeValidation.Visibility = Visibility.Visible;
                return;
            }
            else
            {
                tblkRangeValidation.Visibility = Visibility.Collapsed;
                IsolatedSettings.saveSettings("range", TbxRange.Text);
            }

        }

        private void lockToggleSwitch_Checked(object sender, RoutedEventArgs e)
        {

            bool IsLocationTrackingEnable = SettingsHelper.getBoolValue("GeoLocationStatus");

            if (IsLocationTrackingEnable)
            {
                if (!ApplicationIdleModeHelper.Current.HasUserAgreedToRunUnderLock)
                {
                    MessageBoxResult result = MessageBox.Show("Running under lockscreen can consume battery even when screen is locked and you are not using the application. Click OK to enable", "", MessageBoxButton.OKCancel);
                    if (result == MessageBoxResult.OK)
                    {
                        ApplicationIdleModeHelper.Current.HasUserAgreedToRunUnderLock = true;
                        toggleswitchRunUnderLoock.Content = "On";
                        SettingsHelper.Save("runUnderLock", "true");
                    }
                    else
                    {

                        toggleswitchRunUnderLoock.IsChecked = false;
                        SettingsHelper.Save("runUnderLock", "false");
                    }
                }
                else
                {
                    toggleswitchRunUnderLoock.Content = "On";
                    SettingsHelper.Save("runUnderLock", "true");
                }
            }
            else
            {
                MessageBox.Show("Location tracking must be enabled");
                toggleswitchRunUnderLoock.IsChecked = false;
            }
        }

        private void lockToggleSwitch_Unchecked(object sender, RoutedEventArgs e)
        {
            toggleswitchRunUnderLoock.Content = "Off";
            SettingsHelper.Save("runUnderLock", "false");
        }


    }
}